
from selenium.webdriver.common.by import By
from common.page import Page, IOS, XPATH
from selenium.common.exceptions import NoSuchElementException
from time import sleep


class CommonTools(Page):

    # 评论输入框
    comment_text = (XPATH, "//XCUIElementTypeOther[1]/XCUIElementTypeTextView[1]")

    # 面授附件相关
    attachment_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '附件'")
    add_image = (IOS, "type == 'XCUIElementTypeButton' AND label == 'tupian2'")
    add_photo = (IOS, "type == 'XCUIElementTypeButton' AND label == 'xiangji'")

    def select_from_album(self, n=3):
        self.find_button(string="返回",contains_flag=1).click()
        try:
             self.find_static_text(string="所有照片", contains_flag=1).click()
            #  self.find_image(string='相机胶卷',contains_flag=1).click()
        except NoSuchElementException:
            pass
        for each in self.find_images(string="未选择", attr='name')[-n:][::-1]:
            self.get_frame_center_and_click(each, flag=0)
        self.find_button("完成").click()

    def take_a_photo(self):
        sleep(3)
        # self.find_button("相机").click()
        self.find_xpath('//*[@name="hVideo_take"]').click()   #拍照
        sleep(3)
        self.find_button("完成").click()
        # self.find_button("发布").click()

    def take_ptoto(self):
         sleep(3)
         self.find_xpath('//*[@name="PhotoCapture"]').click()  # 拍照
         sleep(3)
         self.find_button(string="使用照片", contains_flag=1).click()
         sleep(2)


    def take_a_Photo(self):
        sleep(3)
        # self.find_button("相机").click()
        self.find_xpath('//*[@name="PhotoCapture"]').click()   #拍照
        sleep(3)
        self.find_button(string="使用照片",contains_flag=1).click()
        # self.find_button("发布").click()
        sleep(0.5)
        self.find_static_text("Img.jpg").click()
        self.find_button("删除").click()
        self.find_button('xiangji').click()
        sleep(1)
        self.find_xpath('//*[@name="PhotoCapture"]').click()  # 拍照进行预览，重命名
        sleep(3)
        self.find_button("使用照片").click()
        self.find_static_text("Img.jpg").click()
        self.find_button("预览").click()
        self.find_button(string="返回",contains_flag=1).click()
        self.find_static_text("Img.jpg").click()
        self.find_button("重命名").click()
        self.find_static_text("Img.jpg").send_keys("重命名图片")
        self.find_button(string="确定",contains_flag=1).click()

    def tacke_photo(self):
        sleep(3)
        self.find_xpath('//*[@name="PhotoCapture"]').click()  # 拍照
        sleep(3)
        self.find_button(string="使用照片", contains_flag=1).click()
        # self.find_button("发布").click()
        sleep(0.5)

    def Edit_all(self):
        self.find_button("richeditor textcolor icon").click()  # 点击字体颜色
        self.find_element(XPATH, "//XCUIElementTypeCollectionView/XCUIElementTypeCell[3]").click()
        self.find_button("richeditor font icon").click()  # 点击字体大小
        self.find_element(XPATH, "//XCUIElementTypeCollectionView/XCUIElementTypeCell[4]").click()
        self.find_button("richeditor bold icon").click()  # 字体加粗
        self.find_button("richeditor justifyleft icon").click()  # 居中显示
        self.find_element(XPATH, "//XCUIElementTypeCollectionView/XCUIElementTypeCell[2]").click()
        self.find_element(XPATH, "//XCUIElementTypeOther/XCUIElementTypeTextView").send_keys(
            "这是内容，你们先看kanyiaikaknan继续刺激啊哈哈他和她相识是在一个宴会上。那时的她年轻美丽，颇有才华，在学校里是出了名的人物，身边自然不乏追求者，而他却是一个很普通的人，很久以来一直爱慕着她，她自然不知道这一切。宴会结束，他邀请她去喝咖啡，她很吃惊，然而，出于礼貌，她还是答应了。坐在咖啡馆里，两个人之间的气氛很是尴尬，没有什么话题")
        self.find_button("richeditor image icon").click()  # 内容图片上传
        self.find_button("拍照上传").click()
        self.take_ptoto()
        sleep(2)
        self.hide_keyboard()
        sleep(2)
        # self.find_button(string="完成",contains_flag=1).click()
        # self.swipe(328, 23, 328, 23)  # 完成
        # sleep(3)

    def Fujian(self):
        # self.get_frame_center_and_click((IOS, 'type == "XCUIElementTypeButton" AND label == "附件"'))
        self.find_element(XPATH,"//XCUIElementTypeOther/XCUIElementTypeButton[1]").click()   #附件
        # self.find_button("xiangji").click()
        self.find_element(XPATH,"//XCUIElementTypeCell[3]/XCUIElementTypeOther[1]").click()   #相机
        sleep(0.5)
        self.find_xpath('//*[@name="hVideo_take"]').click()  # 拍照
        sleep(3)
        self.find_button("完成").click()
        sleep(2)
        self.find_static_text("Img.jpg").click()
        self.find_button("删除").click()
        # self.find_button('xiangji').click()
        self.find_element(XPATH, "//XCUIElementTypeCell[3]/XCUIElementTypeOther[1]").click()
        sleep(0.5)
        self.find_xpath('//*[@name="hVideo_take"]').click()  # 拍照
        sleep(3)
        self.find_button("完成").click()
        self.find_static_text("Img.jpg").click()
        self.find_button("预览").click()
        self.find_button(string="common back dark icon", contains_flag=1).click()
        self.find_static_text("Img.jpg").click()
        self.find_button("重命名").click()
        self.find_static_text("Img.jpg").send_keys("重命名图片")
        self.find_button(string="确定", contains_flag=1).click()
        sleep(1)


    def ZiJian(self):         #作业自荐
        self.find_static_text(string="自荐",contains_flag=1).click()
        self.find_static_text(string="请输入自荐理由", contains_flag=1).click()
        self.swipe(30, 100, 30, 100)
        self.find_static_text(string="请输入自荐理由", contains_flag=1).send_keys("自荐理由内容显示如下自荐理由内容显示如下")
        self.find_button("保存").click()
        self.find_static_text(string="公开方式", contains_flag=1).click()
        self.find_button("确定").click()
        self.find_button("commontool review unselected").click()
        self.find_button("我同意").click()

    def  PySelect(self):
        self.find_element(XPATH,'//*[@name="批阅状态"]').click()
        self.find_static_text("已批阅").click()
        sleep(2)
        self.find_element(XPATH, '//*[@name="查阅状态"]').click()
        self.find_static_text("未查阅").click()
        sleep(2)
        self.find_element(XPATH, '//*[@name="作业成绩"]').click()
        self.find_static_text("优秀").click()
        sleep(3)
        self.find_element(XPATH, '//*[@name="更多筛选"]').click()
        self.find_element(XPATH,'//*[@name="确定"]').click()

    def Select_btn(self):
        self.find_element(XPATH, '//*[@name="发布人"]').click()
        self.find_static_text("学员").click()
        sleep(2)
        self.find_button("更多筛选").click()
        self.find_element(XPATH, '//*[@name="确定"]').click()
        self.find_static_text("我的").click()
        sleep(3)
        self.find_static_text("草稿").click()
        sleep(4)
        self.find_static_text("编辑").click()
        self.find_button("下一步").click()




    def Select_Biao(self,title='自动化测试文章'):
        self.find_static_text("我相关的文章").click()
        self.find_element(XPATH,'//*[@name="查阅状态"]').click()
        self.find_static_text("未查阅").click()
        self.find_static_text(string=title,contains_flag=1).click()
        self.find_element(XPATH,'//*[@name="smore"]').click()
        self.find_button('查阅').click()
        self.find_element(XPATH,'//*[@name="请填写推荐理由（选填）"]').click()
        self.find_element(XPATH, '//*[@name="请填写推荐理由（选填）"]').send_keys("推荐理由如下，哈哈哈哈哈哈哈哈哈")
        self.find_element(XPATH,'//*[@name="请填写查阅意见（必填）"]').click()
        self.find_element(XPATH,'//*[@name="请填写查阅意见（必填）"]').send_keys("查阅意见我给你特别特别特别特别好的优秀")
        self.hide_keyboard()
        self.find_button("完成").click()
        sleep(3)


    def CaoG(self,title='%s自动化测试文章%d'):
        self.find_button("common release").click()
        self.find_static_text(string="请输入标题", attr='value', contains_flag=1).click()
        self.find_static_text(string="请输入标题", attr='value', contains_flag=1).send_keys("草稿标题")
        self.swipe(10, 140, 10, 140)  # 点击文章内容
        sleep(2)
        self.find_element(XPATH, "//XCUIElementTypeOther/XCUIElementTypeTextView").send_keys(
            "这是草稿内容，你们先看看吧哈哈哈哈哈哈哈哈哈")
        self.hide_keyboard()
        self.find_button("common back dark icon").click()
        self.find_button("保存草稿").click()
        sleep(3)

    def ZIJian(self):
        self.swipe(10,130,10,130)   #自荐理由
        self.find_static_text(string="请输入自荐理由", contains_flag=1).click()
        self.find_static_text(string="请输入自荐理由",contains_flag=1).send_keys("这是自荐理由自荐理由自荐理由自荐理由自荐理由haha ")
        self.find_button("保存").click()
        sleep(1)
        self.find_static_text(string="公开方式", contains_flag=1).click()
        self.find_button("确定").click()
        self.find_button("commontool review unselected").click()
        self.find_button("我同意").click()

